package com.goods.business.service.imp;

import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    //物资资料列表显示
    @Override
    public List<Product> findProductList(String name,Integer status, Long[] categorys) {
        if(categorys != null){
            if (categorys.length == 1) {
                return productMapper.selectExt(name,status,categorys[0],null,null);
            }
            if (categorys.length == 2){
                return productMapper.selectExt(name,status,categorys[0],categorys[1],null);
            }
            if (categorys.length == 3) {
                return productMapper.selectExt(name,status,categorys[0],categorys[1],categorys[2]);
            }
        }
        return productMapper.selectExt(name,status,null,null,null);
    }

    @Override
    public void add(Product product) {
        productMapper.insert(product);
    }

    @Override
    public Product edit(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Long id, Product product) {
        product.setId(id);
        product.setStatus(2);
        productMapper.updateByPrimaryKey(product);
    }


    @Override
    public void remove(Long id) {
        updateStatus(id,1);
    }

    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void back(Long id) {
        updateStatus(id,0);
    }

    @Override
    public void publish(Long id) {
        updateStatus(id,0);
    }

    @Override
    public List<ProductStockVO> findAllProductStocks(String name, Long[] categorys) {
        List<ProductStockVO> productStockVOList = new ArrayList<>();
        List<Product> products = null;
        if(categorys != null || name != null){
            if (categorys.length == 1) {
                products = productMapper.selectExt(name,null,categorys[0],null,null);
            }
            if (categorys.length == 2){
                products = productMapper.selectExt(name,null,categorys[0],categorys[1],null);
            }
            if (categorys.length == 3) {
                products = productMapper.selectExt(name,null,categorys[0],categorys[1],categorys[2]);
            }
        }
        products = productMapper.selectAll();
        products.stream().forEach(product -> {
            ProductStockVO productStockVO = new ProductStockVO();
            //List<InStockInfo> list = instockInfoMapper.selectByPNum(product.getPNum());
            productStockVO.setId(product.getId());
            productStockVO.setName(product.getName());
            productStockVO.setPNum(product.getPNum());
            productStockVO.setModel(product.getModel());
            productStockVO.setUnit(product.getUnit());
            productStockVO.setRemark(product.getRemark());
            productStockVO.setImageUrl(product.getImageUrl());
            ProductStock stock = productMapper.selectStock(product.getPNum());
            if (stock == null){
                productStockVO.setStock(0L);
            }else {
                productStockVO.setStock(stock.getStock());
                productStockVOList.add(productStockVO);
            }
        });
        return productStockVOList;
    }

    //修改状态码
    //0：已通过  1：回收站  2：未审核
    private void updateStatus(Long id,Integer code){
        productMapper.updateStatus(id,code);
    }

}
